<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      label-width="85px"
      class="globalSerchFormStyle"
    >
      <el-row>
        <el-col :span="6">
          <el-form-item label="油企名称" prop="enterpriseId" size="small">
            <el-select
              style="width: 180px"
              v-model="queryParams.enterpriseId"
              :clearable="true"
              filterable
              placeholder="请选择油企"
              @change="handleSelectChange"
            >
              <el-option
                v-for="(item, index) in enterpriseList"
                :key="index"
                :value="item.enterpriseId"
                :label="item.enterpriseName"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="油站名称" prop="stationId" size="small">
            <el-select
              style="width: 180px"
              v-model="queryParams.stationId"
              :clearable="true"
              filterable
              placeholder="请选择油站名称"
            >
              <el-option
                v-for="item in stationList"
                :key="item.stationId"
                :label="item.stationName"
                :value="item.stationId"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="电子卡类型" prop="orderNo">
            <el-input
              style="width: 180px"
              v-model="queryParams.orderNo"
              placeholder="请输入电子卡类型"
              clearable
              @keyup.enter.native="handleQuery"
              size="small"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <div v-show="collapse">
        <el-row>
          <el-col :span="6">
            <el-form-item label="活动状态" prop="likeCustomerPhone">
              <el-input
                style="width: 180px"
                v-model="queryParams.likeCustomerPhone"
                placeholder="请选择活动状态"
                clearable
                @keyup.enter.native="handleQuery"
                size="small"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="创建时间" prop="createdDate" size="small">
              <el-date-picker
                size="small"
                v-model="dateRangeCreatedDate"
                value-format="yyyy-MM-dd HH:mm:ss"
                type="datetimerange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                style="width: 180px"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
      </div>
    </el-form>

    <el-row
      class="global_el_rowStyle"
      :gutter="10"
      style="
        border-top: none;
        border-bottom: 1px solid #f5f5f5;
        margin-top: 0px;
      "
    >
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          plain
          size="mini"
          id="closeSearchBtn"
          @click="
            (closeSearch) => {
              collapse = !collapse;
            }
          "
        >
          {{ collapse ? "收起" : "展开更多" }}
          <i :class="collapse ? 'el-icon-arrow-up ' : 'el-icon-arrow-down'"></i>
        </el-button>
      </el-col>
    </el-row>

    <el-table :data="oilList" stripe>
      <af-table-column
        label="电子卡"
        align="left"
        width="100"
        :formatter="oilNameFormatter"
      />
      <af-table-column label="条件" align="left" prop="discountAmtTerm">
        <template slot-scope="scope1">
          {{ scope1.row.discountAmtStart }}≤充值金额(元)&lt;{{
            scope1.row.discountAmtEnd
          }}
        </template>
      </af-table-column>

      <af-table-column label="赠送" align="left">
        <template slot-scope="scope">
          {{
            scope.row.settingRuleType == 1
              ? "按金额赠送"
              : scope.row.settingRuleType == 2
              ? "按比例赠送"
              : ""
          }}
          --- 额度为 {{ scope.row.presentAmt }}
          {{
            scope.row.settingRuleType == "1"
              ? "元"
              : scope.row.settingRuleType == "2"
              ? "%"
              : ""
          }}
        </template>
      </af-table-column>
      <!-- <af-table-column label="生效状态" align="left">
              <template slot-scope="scope">
                {{ effective(scope.row.cardType) }}
              </template>
            </af-table-column> -->
      <af-table-column
        label="操作"
        align="left"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handlePayEdit(scope.row)"
            size="mini"
            style="margin: 0px 5px"
            >编辑</el-button
          >
          <el-button
            type="text"
            @click="handlePayDel(scope.row)"
            size="mini"
            style="margin: 0px 5px"
            >删除</el-button
          >
        </template>
      </af-table-column>
    </el-table>

    <pagination
      :hidden="oilListTotal > 0 ? false : true"
      :total="oilListTotal"
      :page.sync="oilQuery.pageNum"
      :limit.sync="oilQuery.pageSize"
      @pagination="getList"
      :autoScroll="true"
    />

    <!-- <el-dialog :title="'添加充值赠额'" width="800px" append-to-body>
      <el-form
        ref="oilForm"
        :model="oilForm"
        :rules="rules"
        label-width="80px"
        label-position="top"
      >
        <el-row>
          <el-col :span="11">
            <el-form-item
              label="品名"
              prop="oilName"
              :rules="{
                required: true,
                message: '请填写充值赠额名',
                trigger: 'change',
              }"
            >
              <el-input v-model="oilForm.oilName" placeholder="请输入品名" />
            </el-form-item>
          </el-col>

          <el-col :span="11" :offset="1">
            <el-form-item
              label="类型"
              prop="oilClass"
              :rules="{
                required: true,
                message: '请填写充值赠额类型',
                trigger: 'change',
              }"
            >
              <el-input
                v-model="oilForm.oilClass"
                placeholder="请输入充值赠额类型"
              />
            </el-form-item>
          </el-col>

          <el-col :span="11">
            <el-form-item
              label="型号"
              prop="oilType"
              :rules="{
                required: true,
                message: '请填写充值赠额型号',
                trigger: 'change',
              }"
            >
              <el-input v-model="oilForm.oilType" placeholder="请输入型号" />
            </el-form-item>
          </el-col>

          <el-col :span="11" :offset="1">
            <el-form-item
              label="单位"
              prop="unitCode"
              :rules="{
                required: true,
                message: '请填写计量单位',
                trigger: 'change',
              }"
            >
              <el-select
                v-model="oilForm.unitCode"
                placeholder="请选择单位"
                clearable
                :style="{ width: '100%' }"
              >
                <el-option label="升" value="1"></el-option>
                <el-option label="千克" value="2"></el-option>
                <el-option label="公斤" value="3"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" size="mini" @click="submitForm"
          >确 定</el-button
        >
        <el-button size="mini" @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="title" :visible.sync="oilOpen" append-to-body>
      <el-form
        ref="oilForm"
        :model="oilForm"
        :rules="rules"
        label-width="120px"
      >
        <el-form-item label="电子卡" prop="cardType">
          <el-select
            v-model="oilForm.cardType"
            placeholder="请选择电子卡"
            clearable
            size="mini"
          >
            <el-option label="汽油卡" value="1"></el-option>
            <el-option label="柴油卡" value="2"></el-option>
            <el-option label="LNG卡" value="3"></el-option>
            <el-option label="CNG卡" value="4"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="条件设置" prop="discountAmtEnd">
          <el-input-number
            :min="0"
            v-model="oilForm.discountAmtStart"
            size="mini"
            style="width: 120px"
          />
          <span>≤ 充值金额(元) &lt;</span>
          <el-input-number
            :min="0"
            v-model="oilForm.discountAmtEnd"
            size="mini"
            style="width: 120px"
          />
        </el-form-item>
        <el-form-item label="赠送方式" prop="presentAmt">
          <div>
            <el-select
              v-model="oilForm.settingRuleType"
              placeholder="请选择赠送方式"
              size="mini"
            >
              <el-option label="按金额赠送" value="1"></el-option>
              <el-option label="按比例赠送" value="2"></el-option>
            </el-select>
            <div v-show="!!this.oilForm.settingRuleType">
              <el-input
                v-model="oilForm.presentAmt"
                style="width: 100px"
                size="mini"
              />
              <span>{{
                this.oilForm.settingRuleType == "1"
                  ? "元"
                  : this.oilForm.settingRuleType == "2"
                  ? "%"
                  : ""
              }}</span>
            </div>
          </div>
        </el-form-item>
        <el-form-item>
          <div>
            <div style="color: red">例：</div>
            <div style="color: red">按赠送金额：充100赠送10元</div>
            <div style="color: red">按赠送比例：充100赠送10%</div>
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">添加</el-button>
      </div>
    </el-dialog> -->
  </div>
</template>

<script>
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { regionData } from "element-china-area-data";
import { getToken } from "@/utils/auth";
import { listEnterpriseStation } from "@/api/dept";
import { enterpriseList } from "@/api/station/order";
import {
  getCardActivityList,
  addActivityList,
  upActivityList,
  delActivityList,
} from "@/api/customer/cardSetting";

export default {
  name: "Dept",
  components: { Treeselect },
  data() {
    const validateDiscountAmtEnd = (rule, value, callback) => {
      if (!this.oilForm.discountAmtStart) {
        callback(new Error("请填写条件的开始金额"));
        return;
      }
      if (!this.oilForm.discountAmtEnd) {
        callback(new Error("请填写条件的结束金额"));
        return;
      }
      const start = +this.oilForm.discountAmtStart;
      const end = +this.oilForm.discountAmtEnd;
      if (start >= end) {
        callback(new Error("最小金额不能大于或等于最大金额"));
        return;
      }
      callback();
    };
    const validateCardType = (rule, value, callback) => {
      if (!this.infoForm.cardType) {
        callback(new Error("请选择电子卡"));
        return;
      }
      callback();
    };
    const validatePresentAmt = (rule, value, callback) => {
      if (!this.oilForm.settingRuleType) {
        callback(new Error("请选择赠送方式"));
        return;
      }
      if (!this.oilForm.presentAmt) {
        callback(new Error("请填写赠送值"));
        return;
      }
      callback();
    };
    return {
      // 查询参数
      queryParams: {
        deptName: undefined,
        status: undefined,
        stationId: null,
      },
      // 表单校验
      rules: {
        cardType: [
          { required: true, message: "请选择电子卡", trigger: "change" },
        ],
        discountAmtEnd: [
          {
            required: true,
            validator: validateDiscountAmtEnd,
            trigger: "change",
          },
        ],
        presentAmt: [
          { required: true, validator: validatePresentAmt, trigger: "change" },
        ],
      },
      oilList: [],
      oilLoading: false,
      oilOpen: false,
      oilForm: {
        oilClass: "", //类型
        oilName: "", // 品名
        oilType: "", //型号
        unitCode: "1", // 单位 1L 2kg 3公斤
      },
      oilQuery: {
        pageSize: 10,
        pageNum: 1,
        oilName: "",
      },
      oilListTotal: 0,
      oilForm: {},
      collapse: false,
      enterpriseList: [],
      stationList: [],
      dateRangeCreatedDate: [],
    };
  },
  created() {
    this.getList();
    this.getEnterpriseList();
    // 如果当前是油企账号，则获取该企业下的油站
    if (this.presentEnterprise && this.presentEnterprise.enterpriseId) {
      this.getStationList(this.presentEnterprise.enterpriseId);
    }
  },
  methods: {
    // 根据油企获取油站
    handleSelectChange(value) {
      this.stationList = [];
      this.queryParams.stationId = null;
      this.getStationList(value);
    },
    // 查询油企
    getEnterpriseList() {
      enterpriseList().then((res) => {
        this.enterpriseList = res.rows;
      });
    },
    getStationList(val) {
      listEnterpriseStation({ enterpriseId: val }).then((res) => {
        this.stationList = res;
      });
    },
    oilNameFormatter(row, column) {
      if (row.cardType === "1") {
        return "汽油卡";
      } else if (row.cardType === "2") {
        return "柴油卡";
      } else if (row.cardType === "3") {
        return "LNG卡";
      } else if (row.cardType === "4") {
        return "CNG卡";
      } else {
        return row.cardType;
      }
    },
    handlePayAdd() {
      this.oilForm = {};
      this.oilOpen = true;
    },
    handlePayDel(row) {
      this.$confirm("确认删除此充值赠额？", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return delActivityList(row);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除充值赠额成功");
        });
    },
    handlePayEdit(row) {
      this.oilForm = JSON.parse(JSON.stringify(row));
      this.oilOpen = true;
    },
    getList() {
      this.oilLoading = true;
      getCardActivityList(this.oilQuery)
        .then((res) => {
          this.oilListTotal = res.total;
          this.oilList = res.rows;
        })
        .finally(() => (this.oilLoading = false));
    },
    cancel() {
      this.oilOpen = false;
    },
    reset() {
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      console.log(11111111111);
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("oilQuery");
      this.handleQuery();
    },

    /** 提交按钮 */
    submitForm: function () {
      this.$refs["oilForm"].validate((valid) => {
        if (valid) {
          if (this.oilForm.id != undefined) {
            upActivityList(this.oilForm).then((res) => {
              this.msgSuccess("更新成功");
              this.getList();
              this.oilOpen = false;
            });
          } else {
            addActivityList(this.oilForm).then((res) => {
              this.msgSuccess("新增成功");
              this.getList();
              this.oilOpen = false;
            });
          }
        }
      });
    },
  },
};
</script>
<style lang="scss">
.card-table {
  margin-top: 40px;
  background-color: #f8f8f8;

  .el-button {
    margin-bottom: 20px;
    margin-right: 20px;
  }
}

::v-deep .el-form-item {
  .el-form-item__label {
    padding: 0;
    margin: 0;
  }

  .el-button--cyan {
    margin-left: 20px;
  }
}
</style>
